home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / dirent.lha / dirent / readdir.c < prev    next >
C/C++ Source or Header  |  1990-09-13  |  1KB  |  57 lines

  1. /*
  2.     readdir -- read next entry from a directory stream
  3.  
  4.     last edit:    25-Apr-1987    D A Gwyn
  5. */
  6.  
  7. #ifdef unos
  8. #include    <errno.h>
  9. #else
  10. #include    <sys/errno.h>
  11. #endif
  12. #include    <sys/types.h>
  13. #include    <dirent.h>
  14.  
  15. extern int    getdents();        /* SVR3 system call, or emulation */
  16.  
  17. extern int    errno;
  18.  
  19. #ifndef NULL
  20. #define    NULL    0
  21. #endif
  22.  
  23. struct dirent *
  24. readdir( dirp )
  25.     register DIR        *dirp;    /* stream from opendir() */
  26. {
  27.     register struct dirent    *dp;    /* -> directory data */
  28.  
  29.     if ( dirp == NULL || dirp->dd_buf == NULL )
  30.         {
  31.         errno = EFAULT;
  32.         return NULL;        /* invalid pointer */
  33.         }
  34.  
  35.     do    {
  36.         if ( dirp->dd_loc >= dirp->dd_size )    /* empty or obsolete */
  37.             dirp->dd_loc = dirp->dd_size = 0;
  38.  
  39.         if ( dirp->dd_size == 0    /* need to refill buffer */
  40.           && (dirp->dd_size =
  41.             getdents( dirp->dd_fd, dirp->dd_buf, (unsigned)DIRBUF )
  42.              ) <= 0
  43.            )
  44.             return NULL;    /* EOF or error */
  45.  
  46.         dp = (struct dirent *)&dirp->dd_buf[dirp->dd_loc];
  47.         dirp->dd_loc += dp->d_reclen;
  48.         }
  49. #ifdef unos
  50.     while ( dp->d_ino < (ino_t) 0 );    /* don't rely on getdents() */
  51. #else
  52.     while ( dp->d_ino == (ino_t) 0 );    /* don't rely on getdents() */
  53. #endif
  54.  
  55.     return dp;
  56. }
  57.